/*******************************************************************
 * 外语翻译平台(translate) 
 *
 * 成都一亿信息技术公司 版权所有
 * 
 * Copyright (C) 2010 Transer Information Technology Co., Ltd.
 *
 * @Version : 1.0  2010-12-26 下午07:51:37
 *
 * SendMailJob.java 
 *
 *******************************************************************/
package com.transer.job;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.log4j.Logger;

import com.transer.console.ConsoleConstants;
import com.transer.persistance.bean.User;
import com.transer.persistance.dao.DatabaseDao;
import com.transer.persistance.dao.IUserDao;
import com.transer.utils.JobQueueOB;
import com.transer.utils.Utils;

/**
 * class defination are there
 * 
 * @author Mark
 * 
 * @version 1.0 ywengineer@gmail.com
 * 
 */
public class ReSendMailJob implements Runnable {
	// 收件人
	private String email;
	// 数据库操作
	private DatabaseDao database;

	public ReSendMailJob(DatabaseDao dao, String mailTo) {
		this.database = dao;
		this.email = mailTo;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Runnable#run()
	 */
	public void run() {
		try {
			// 如果数据库操作或者邮件为空,返回
			if (database == null || StringUtils.isEmpty(email)) {
				return;
			}
			// 用户操作
			IUserDao userDao = (IUserDao) database;
			// 获取用户
			User user = userDao.isEmailExsit(email);
			// 如果不存在用户,返回
			// 用户已激活,返回
			if (user == null || user.isActive()) {
				return;
			}
			// 生成激活码
			user.setActiveCode(Utils.getUUID());
			// 修改用户
			userDao.updateUser(user);
			// 发送邮件
			JobQueueOB.getInstance().addJob(ConsoleConstants.QUEUE_INDEX_MAIL, new SendMailJob(user));
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("重新生成激活码更新用户失败", e);
		} finally {
			database = null;
		}
	}

	private static final Logger logger = Logger.getLogger(ReSendMailJob.class);
}
